<?php

namespace App\Http\Requests;

use App\Exceptions\exception\ApiExcetion;
use App\model\Coustomer;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Input;

class CoustomerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
           // 'industry_id'=>'required',
            'name'=>'required',
            'mobile'=>'required',

            'desc'=>'required',
            'source_id'=>'required',
            'project_id'=>'required',
            'area_id'=>'required',
            'demand_id'=>'required',
            'city1'=>'required',
            'city2'=>'required'
        ];
    }
    public function messages()
    {
        return [
            //'industry_id.required'=>'请选择行业',
            'mobile.required'=>'请输入联系方式',
            'mobile.unique'=>'unique',
            'name.required'=>'请输入姓名',
            'desc.required'=>'请输入备注',
            'source_id.required'=>'请选择来源',
            'area_id.required'=>'请选择面积',
            'demand_id.required'=>'请选择需求',
        ];
    }
    public function withValidator($validator)
    {
        $validator->sometimes('mobile', 'unique:customer', function ($input) {
            return Coustomer::whereProjectId($input->project_id)->where('mobile','=',$input->mobile)->exists();
        });
    }
    protected function failedValidation(\Illuminate\Contracts\Validation\Validator $validator)
    {
        throw new ApiExcetion($validator->errors()->first());
    }
}
